import { LibAppView } from "../../../lib";
import { LoginReducer } from "./Login";

interface IAdminBreadcrumb {
    title: string|React.ReactNode;
    pathname: string;
    hasChildren: boolean;
}

interface IAdminState {
    systemName: string;
    systemNameAbbr: string;
    systemMenus: Admin.IAdminMenu[];
    selectedMenu: string;
    breadcrumbData: IAdminBreadcrumb[];
    isLoadSystemMenu: boolean;
};

const { createReducer, createCombineReducer } = LibAppView;

const defaultMenus: Admin.IAdminMenu[] = [
    {
        label: "admin.menu.main",
        icon: "HomeOutlined",
        pathname: "/admin/main",
        value: "adminHomePage"
    }
];

export const AdminLayoutReducer = createReducer<IAdminState, {}>({
    initState: {
        systemName: "admin.layout.logo.full",
        systemNameAbbr: "admin.layout.logo.abbr",
        systemMenus: [...defaultMenus],
        selectedMenu: "adminHomePage",
        breadcrumbData: [{
            title: "admin.menu.main", 
            pathname: "/admin/main",
            hasChildren: false
        }],
        isLoadSystemMenu: false
    },
    name: "admin",
    handler: (state, option) => {
        const updateState = {
            ...state
        };
        switch(option.action.type) {
            case "Update_System_Menu": {
                return {
                    ...updateState,
                    systemMenus: [...defaultMenus, ...option.action.data.systemMenus],
                    isLoadSystemMenu: true
                }
            }
            case "Update_Breadcrumb": {
                return {
                    ...updateState,
                    breadcrumbData: option.action.data.breadcrumbData
                }
            }
            default: {
                return updateState;
            }
        }
    }
});

export const AdminReducer = createCombineReducer(AdminLayoutReducer, {
    login: LoginReducer
});
